package org.jash.sportnews2109.ui

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import dagger.hilt.android.AndroidEntryPoint
import org.jash.common.mvvm.BaseActivity
import org.jash.common.viewModels
import org.jash.sportnews2109.R
import org.jash.sportnews2109.databinding.ActivityPlayerBinding
import org.jash.sportnews2109.entity.Video
import org.jash.sportnews2109.utils.VideoLifecycle
import org.jash.sportnews2109.vm.LoginViewModel
import org.jash.sportnews2109.vm.PlayerViewModel

@Route(path = "/news/player")
@AndroidEntryPoint
class PlayerActivity : BaseActivity<ActivityPlayerBinding, PlayerViewModel>() {
    @Autowired
    @JvmField
    var id = 0
    override fun initView() {
        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
//        val vm :LoginViewModel = ViewModelProvider(this)[LoginViewModel::class.java]
        val vm :LoginViewModel = viewModels()
    }

    override fun initData() {
//        val indent = Intent(this, LoginActivity::class.java)
//        indent.putExtra("id", id)
//        startActivity(indent)
//        startActivity(Intent(this, LoginActivity::class.java).apply {
//            putExtra("id", id)
//        })
        ARouter.getInstance().inject(this)
        viewModel.videoLiveData.observe(this, this::load)
        viewModel.loadVideo(id)
        lifecycle.addObserver(VideoLifecycle(binding.player))
    }
    fun load(video: Video) {
        binding.toolbar.title = video.title
        binding.player.setUp(video.videourl, true, video.title)
    }

    override fun onStop() {
        super.onStop()
        binding.player.onVideoPause()
    }
    override fun onDestroy() {
        super.onDestroy()
        binding.player.release()
    }

}